package ar.view.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import ar.activity.R;

public class HomeAdapter extends BaseAdapter {

	private Context mContext;
	private List<Integer> mIcons;
	private List<Bitmap> mBitmaps;
	private Resources res;

	public HomeAdapter(Context context) {
		this.mContext = context;
		res = context.getResources();
		this.mIcons = new ArrayList<Integer>();
		this.mBitmaps = new ArrayList<Bitmap>();
		this.mIcons.add(R.drawable.armap_icon);
		this.mIcons.add(R.drawable.share_icon);
		this.mIcons.add(R.drawable.datasource_icon);
		this.mIcons.add(R.drawable.datafilter_icon);
		this.mIcons.add(R.drawable.settings_icon);
		this.mIcons.add(R.drawable.about_icon);
		
		initBitmap();
	}

	@Override
	public int getCount() {
		return this.mIcons.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView == null) {
			imageView = new ImageView(this.mContext);
			imageView.setLayoutParams(new GridView.LayoutParams(
					LayoutParams.FILL_PARENT, 155));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
		} else {
			imageView = (ImageView) convertView;
		}

		imageView.setImageBitmap(mBitmaps.get(position));
		return imageView;
	}
	
	public void initBitmap() {
		int nBitmaps = mIcons.size();
		for(int i = 0; i < nBitmaps; i ++) {
			mBitmaps.add(BitmapFactory.decodeResource(res, mIcons.get(i)));
		}
	}
	
	public void realeaseBitmap() {
		if(mBitmaps != null && mBitmaps.size() > 0) {
			int nBitmaps = mBitmaps.size();
			for(int i = 0; i < nBitmaps; i ++) {
				Bitmap bmp = mBitmaps.get(i);
				if(bmp != null) {
					bmp.recycle();
				}
			}
			mBitmaps.clear();
		}
	}
}
